package org.black.phoneUtility.sms.reply.widget;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFrom;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbers;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbersInMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromAllNumbersNotInMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromChooseFromMyContacts;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromMatchSmsWithKeyword;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromNumbersFromContactGroup;
import org.black.phoneUtility.sms.reply.database.dataModel.ResponseNumberFromUserInput;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ResponseNumberFromAdapter extends BaseAdapter {
	private static final String LOG_TAG = ResponseNumberFromAdapter.class
			.getName();

	final static class ViewHolder {
		RelativeLayout singleRowRelativelayout;
		TextView singleRowTitle;
		RelativeLayout doubleRowRelativeLayout;
		TextView doubleRowTitle;
		TextView doubleRowSummary;
	}

	private List<ResponseNumberFrom> responseNumberFroms;
	private Map<Integer, Boolean> selectionItems;
	private Context context;
	private LayoutInflater layoutInflater;

	@SuppressLint("UseSparseArrays")
	public ResponseNumberFromAdapter(Context context) {
		this.context = context;
		this.layoutInflater = LayoutInflater.from(this.context);
		this.responseNumberFroms = new ArrayList<ResponseNumberFrom>();
		this.selectionItems = new HashMap<Integer, Boolean>();
	}

	public ResponseNumberFromAdapter(Context context,
			List<ResponseNumberFrom> responseNumberFroms) {
		this(context);
		this.responseNumberFroms = responseNumberFroms;
	}

	public void setResponseNumberFroms(
			List<ResponseNumberFrom> responseNumberFroms) {
		this.responseNumberFroms = responseNumberFroms;
	}

	public boolean add(ResponseNumberFrom responseNumberFrom) {
		if (this.responseNumberFroms == null) {
			this.responseNumberFroms = new ArrayList<ResponseNumberFrom>();
		}
		boolean result = this.responseNumberFroms.add(responseNumberFrom);
		this.notifyDataSetChanged();
		return result;

	}

	public boolean add(List<ResponseNumberFrom> responseNumberFroms) {
		boolean result = false;
		if (responseNumberFroms != null && this.responseNumberFroms != null) {
			result = this.responseNumberFroms.addAll(responseNumberFroms);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public boolean remove(ResponseNumberFrom responseNumberFrom) {
		boolean result = false;
		if (this.responseNumberFroms != null) {
			result = this.responseNumberFroms.remove(responseNumberFrom);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public boolean remove(List<ResponseNumberFrom> responseNumberFroms) {
		boolean result = false;
		if (responseNumberFroms != null && this.responseNumberFroms != null) {
			this.responseNumberFroms.removeAll(responseNumberFroms);
			this.notifyDataSetChanged();
		}
		return result;
	}

	public void addSelection(int position, boolean checked) {
		this.selectionItems.put(position, checked);
		this.notifyDataSetChanged();
	}

	public void removeSelection(int position) {
		this.selectionItems.remove(position);
		this.notifyDataSetChanged();
	}

	public void removeSelections() {
		List<ResponseNumberFrom> responseNumberFroms = new ArrayList<ResponseNumberFrom>();
		Set<Integer> keys = this.selectionItems.keySet();
		for (Integer key : keys) {
			boolean checked = this.selectionItems.get(key);
			if (checked == true) {
				responseNumberFroms.add(this.responseNumberFroms.get(key));
			}
		}
		this.responseNumberFroms.removeAll(responseNumberFroms);
		this.notifyDataSetChanged();
	}

	public void clearSelection() {
		this.selectionItems.clear();
		this.notifyDataSetChanged();
	}

	public boolean isPositionChecked(int position) {
		Boolean result = this.selectionItems.get(position);
		return result == null ? false : result;
	}

	@Override
	public int getCount() {
		if (this.responseNumberFroms == null) {
			return 0;
		}

		return this.responseNumberFroms.size();
	}

	@Override
	public Object getItem(int position) {
		if (this.responseNumberFroms == null) {
			return null;
		}

		return this.responseNumberFroms.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		ResponseNumberFrom responseNumberFrom = this.responseNumberFroms
				.get(position);
		if (convertView == null) {
			convertView = this.layoutInflater.inflate(
					R.layout.sms_response_number_from_row, null);
			viewHolder = new ViewHolder();
			viewHolder.singleRowRelativelayout = (RelativeLayout) convertView
					.findViewById(R.id.smsResponseNumberFromRowNumberInformationSingleRowPanelRelativeLayout);
			viewHolder.singleRowTitle = (TextView) convertView
					.findViewById(R.id.smsResponseNumberFromRowNumberInformationSingleRowTextView);
			viewHolder.doubleRowRelativeLayout = (RelativeLayout) convertView
					.findViewById(R.id.smsResponseNumberFromRowNumberInformationTwoRowsPanelRelativeLayout);
			viewHolder.doubleRowTitle = (TextView) convertView
					.findViewById(R.id.smsResponseNumberFromRowNumberInformationTowRowsTitleTextView);
			viewHolder.doubleRowSummary = (TextView) convertView
					.findViewById(R.id.smsResponseNumberFromRowNumberInformationTowRowsDetailTextView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		if (responseNumberFrom instanceof ResponseNumberFromChooseFromMyContacts
				|| responseNumberFrom instanceof ResponseNumberFromAllNumbers
				|| responseNumberFrom instanceof ResponseNumberFromAllNumbersInMyContacts
				|| responseNumberFrom instanceof ResponseNumberFromAllNumbersNotInMyContacts
				|| responseNumberFrom instanceof ResponseNumberFromUserInput
				|| responseNumberFrom instanceof ResponseNumberFromMatchSmsWithKeyword
				|| responseNumberFrom instanceof ResponseNumberFromNumbersFromContactGroup) {

			if (responseNumberFrom instanceof ResponseNumberFromAllNumbers
					|| responseNumberFrom instanceof ResponseNumberFromAllNumbersInMyContacts
					|| responseNumberFrom instanceof ResponseNumberFromAllNumbersNotInMyContacts
					|| responseNumberFrom instanceof ResponseNumberFromUserInput
					|| responseNumberFrom instanceof ResponseNumberFromMatchSmsWithKeyword) {
				viewHolder.singleRowRelativelayout.setVisibility(View.VISIBLE);
				viewHolder.doubleRowRelativeLayout.setVisibility(View.GONE);

				String title = null;
				if (responseNumberFrom instanceof ResponseNumberFromAllNumbers) {
					title = this.context
							.getString(R.string.sms_add_edit_response_number_from_number_from_all_numbers);
				} else if (responseNumberFrom instanceof ResponseNumberFromAllNumbersInMyContacts) {
					title = this.context
							.getString(R.string.sms_add_edit_response_number_from_number_from_all_numbers_in_contacts);
				} else if (responseNumberFrom instanceof ResponseNumberFromAllNumbersNotInMyContacts) {
					title = this.context
							.getString(R.string.sms_add_edit_response_number_from_number_from_all_numbers_not_in_contacts);
				} else if (responseNumberFrom instanceof ResponseNumberFromUserInput) {
					ResponseNumberFromUserInput responseNumberFromUserInput = (ResponseNumberFromUserInput) responseNumberFrom;
					title = PhoneNumberUtils
							.formatNumber(responseNumberFromUserInput
									.getPhoneNumber());
				} else {
					ResponseNumberFromMatchSmsWithKeyword responseNumberFromMatchSmsWithKeyword = (ResponseNumberFromMatchSmsWithKeyword) responseNumberFrom;
					title = this.context
							.getString(R.string.sms_add_edit_response_number_from_number_from_sms_messages_contains_keyword_keyword)
							+ responseNumberFromMatchSmsWithKeyword
									.getKeyword();
				}
				viewHolder.singleRowTitle.setText(title);
			} else {
				viewHolder.singleRowRelativelayout.setVisibility(View.GONE);
				viewHolder.doubleRowRelativeLayout.setVisibility(View.VISIBLE);
				if (responseNumberFrom instanceof ResponseNumberFromChooseFromMyContacts) {
					ResponseNumberFromChooseFromMyContacts responseNumberFromContact = (ResponseNumberFromChooseFromMyContacts) responseNumberFrom;
					viewHolder.doubleRowTitle.setText(responseNumberFromContact
							.getName());
					viewHolder.doubleRowSummary.setText(PhoneNumberUtils
							.formatNumber(responseNumberFromContact
									.getPhoneNumber()));
				} else if (responseNumberFrom instanceof ResponseNumberFromNumbersFromContactGroup) {
					ResponseNumberFromNumbersFromContactGroup responseNumberFromNumbersFromContactGroup = (ResponseNumberFromNumbersFromContactGroup) responseNumberFrom;
					viewHolder.doubleRowTitle
							.setText(responseNumberFromNumbersFromContactGroup
									.getName()
									+ " ( "
									+ responseNumberFromNumbersFromContactGroup
											.getGroupName() + " ) ");
					viewHolder.doubleRowSummary
							.setText(PhoneNumberUtils
									.formatNumber(responseNumberFromNumbersFromContactGroup
											.getPhoneNumber()));
				}
			}
		} else {
			Log.e(LOG_TAG,
					"Serious problem! responseNumberFrom does not match any type");
		}
		if (this.selectionItems.get(position) != null) {
			convertView.setBackgroundColor(this.context.getResources()
					.getColor(android.R.color.holo_blue_light));
		} else {
			convertView.setBackground(this.context.getResources().getDrawable(
					android.R.drawable.list_selector_background));
		}
		return convertView;
	}

}
